package com.jspgou.cms.action.admin.main;

import static com.jspgou.common.page.SimplePage.cpn;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import antlr.StringUtils;

import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.CookieUtils;
import com.jspgou.core.entity.Website;
import com.jspgou.core.web.SiteUtils;
import com.jspgou.core.web.WebErrors;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.SalesStatistic;
import com.jspgou.cms.manager.OrderMng;
import com.jspgou.cms.manager.SalesStatisticMng;

@Controller
public class SalesStatisticAct {
	private static final Logger log = LoggerFactory.getLogger(SalesStatisticAct.class);

	@RequestMapping("/salesstatistic/v_list.do")
	public String list(Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(cpn(pageNo), CookieUtils
				.getPageSize(request));
		List<Order> orders=orderMng.getCountByStatus(null,null,null);
		List<Order> notOrders=orderMng.getCountByStatus(null,null,3);
		model.addAttribute("notOrders", notOrders);	
	    Integer flag=1;
	
		model.addAttribute("flag", flag);
			
		model.addAttribute("pagination",pagination);
		model.addAttribute("pageNo",pageNo);
		model.addAttribute("orders", orders);
		
		return "salesstatistic/list";
	}

	@RequestMapping("/salesstatistic/v_add.do")
	public String add(ModelMap model) {
		return "salesstatistic/add";
	}

	@RequestMapping("/salesstatistic/v_edit.do")
	public String edit(Long id, Integer pageNo, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		model.addAttribute("salesStatistic", manager.findById(id));
		model.addAttribute("pageNo", pageNo);
		return "salesstatistic/edit";
	}

	@RequestMapping("/salesstatistic/o_save.do")
	public String save(SalesStatistic bean, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean);
		log.info("save SalesStatistic id={}", bean.getId());
		return "redirect:v_list.do";
	}

	@RequestMapping("/salesstatistic/o_update.do")
	public String update(SalesStatistic bean, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.update(bean);
		log.info("update SalesStatistic id={}.", bean.getId());
		return list(pageNo, request, model);
	}

	@RequestMapping("/salesstatistic/o_delete.do")
	public String delete(Long[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		SalesStatistic[] beans = manager.deleteByIds(ids);
		for (SalesStatistic bean : beans) {
			log.info("delete SalesStatistic id={}", bean.getId());
		}
		return list(pageNo, request, model);
	}
	
	
	/*
	 * 按时间搜索
	 * 
	 */
	@RequestMapping("/salesStatistic/refer.do")	
	public String refer( Integer flag,SalesStatistic bean, Integer pageNo,Date startTime,Date endTime,
			HttpServletRequest request,ModelMap model){	
		Website web = SiteUtils.getWeb(request);
		Calendar calendar=Calendar.getInstance();// 创建 Calendar日期 对象
		//默认一个月
		if(startTime==null&&endTime==null){
			endTime=calendar.getTime();
			calendar.add(Calendar.MONTH, -1);			
			startTime=calendar.getTime();
		} 
     
		if(flag==1){
			//选择日期统计	
	       List <Order> orders=orderMng.getCountByStatus(startTime, endTime, null);	
		   model.addAttribute("orders",orders);
		}else if(flag==2){
			int y;
			if(request.getParameter("year")!=null && !request.getParameter("year").equals("")){
				y = Integer.parseInt(request.getParameter("year"));
			
			}else{
				
			 	y= calendar.get(Calendar.YEAR);
			 	
			  	
			}
			 List<Order> orders=orderMng.getStatisticByYear(y,null);
			 model.addAttribute("orders",orders);
			 model.addAttribute("year", y);
		}
		model.addAttribute("flag",flag);
		model.addAttribute("startTime",startTime);
		model.addAttribute("endTime",endTime);
		return "salesstatistic/list";
	}
	
	

	
	private WebErrors validateSave(SalesStatistic bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		/*bean.setWebsite(web);*/
		return errors;
	}
	
	private WebErrors validateEdit(Long id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		if (vldExist(id, web.getId(), errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Long id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		if (vldExist(id, web.getId(), errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateDelete(Long[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		Website web = SiteUtils.getWeb(request);
		errors.ifEmpty(ids, "ids");
		for (Long id : ids) {
			vldExist(id, web.getId(), errors);
		}
		return errors;
	}

	private boolean vldExist(Long id, Long webId, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		SalesStatistic entity = manager.findById(id);
		if(errors.ifNotExist(entity, SalesStatistic.class, id)) {
			return true;
		}
		/*if (!entity.getWebsite().getId().equals(webId)) {
			errors.notInWeb(SalesStatistic.class, id);
			return true;
		}*/
		return false;
	}
	
	@Autowired
	private SalesStatisticMng manager;
	@Autowired
    private OrderMng orderMng;
}